<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix" style="border: none">
      <el-tabs v-model="activeName" class="tab">
        <el-tab-pane label="销售额" name="sale" />
        <el-tab-pane label="访问量" name="viste" />
      </el-tabs>
      <div style="position: absolute; right: 0px;" class="right">
        <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <el-date-picker
          v-model="date"
          class="date-picker"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="mini"
          value-format="yyyy-MM-dd"
          :default-time="['00:00:00', '23:59:59']"
        />
      </div>
    </div>
    <div>
      <el-row :gutter="10">
        <el-col :span="18">
          <div ref="charts" class="charts" />
        </el-col>
        <el-col :span="6">
          <h3 style="margin: 0px;">门店{{ title }}排名</h3>
          <ul>
            <li>
              <span class="rindex">1</span>
              <span class="text">肯德基</span>
              <span class="rvalue">123455</span>
            </li>
            <li>
              <span class="rindex">2</span>
              <span class="text">肯德基</span>
              <span class="rvalue">123455</span>
            </li>
            <li>
              <span class="rindex">3</span>
              <span class="text">肯德基</span>
              <span class="rvalue">123455</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
import * as echarts from 'echarts'
import dayjs from 'dayjs'
export default {
  name: 'Sale',
  data() {
    return {
      activeName: 'sale',
      myCharts: null,
      date: []
    }
  },
  computed: {
    // 计算属性-标题
    title() {
      return this.activeName === 'sale' ? '销售额' : '访问量'
    }
  },
  watch: {
    title() {
      // 重新修改图标的配置数据
      this.myCharts.setOption({
        title: {
          text: this.title + '趋势'
        }
      })
    }
  },
  mounted() {
    this.myCharts = echarts.init(this.$refs.charts)
    this.myCharts.setOption({
      title: {
        text: this.title + '趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: [180, 52, 200, 334, 390, 330, 220, 152, 180, 300, 50, 180, 260]
        }
      ]
    })
  },
  methods: {
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    }
  }
}
</script>

<style scoped>
  .box-card {
    margin: 10px 0PX;
  }
  .clearfix{
    position: relative;
    display: flex;
    justify-content: space-between;
  }
  .tab{
    width: 100%
  }
  .right span{
    margin: 0px 10px;
    cursor: pointer;
  }
  .date-picker{
    width: 240px;
  }
  >>>.el-card__header{
    border: none;
    padding-bottom: 0px;
  }
  >>>.el-card__body{
    padding-top: 0px;
  }
  .charts{
    width: 100%;
    height: 300px;
  }
  ul{
    list-style: none;
    width: 100%;
    padding: 0px;
  }
  ul li{
    margin-bottom: 10px;
  }
  ul li span{
    line-height: 20px;
  }
  .rindex{
    float: left;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: black;
    color: white;
    text-align: center;
  }
  .text{
    margin: 0px 5px;
  }
  .rvalue{
    float: right;
  }
</style>
